#include "folderindex.h"
#include "ui_folderindex.h"

folderIndex::folderIndex(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::folderIndex)
{
    ui->setupUi(this);

    createActions();
    initActions();
    initSysTray();

    // create settings form
    myViewsForm = new viewModeSettingsForm( this );
    notifyShown = false;
}

folderIndex::~folderIndex()
{
    delete ui;
}

void folderIndex::createActions()
{
    showAction = new QAction(tr("&Show"), this);
    connect(showAction, SIGNAL(triggered()), this, SLOT(showNormal()));
}

void folderIndex::initActions()
{
    connect(ui->actionExit, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(ui->actionIndex_Folder, SIGNAL(triggered()), this, SLOT(on_indexFolderButton_clicked()));
    connect(ui->actionIndexFtp, SIGNAL(triggered()), this, SLOT(on_indexFtpButton_clicked()));
    connect(ui->actionIndex_Folder_list, SIGNAL(triggered()), this, SLOT(on_indexListButton_clicked()));
}

void folderIndex::initSysTray()
{
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(showAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction( ui->actionIndex_Folder );
    trayIconMenu->addAction( ui->actionIndexFtp );
    trayIconMenu->addAction( ui->actionIndex_Folder_list );
    trayIconMenu->addSeparator();
    trayIconMenu->addAction( ui->actionSettings );
    trayIconMenu->addSeparator();
    trayIconMenu->addAction( ui->actionExit );

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon( QIcon(":/images/folder.png") );
    trayIcon->setContextMenu(trayIconMenu);
    trayIcon->show();

    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT( iconActivated(QSystemTrayIcon::ActivationReason) ));
}

void folderIndex::on_indexListButton_clicked()
{
    foldersListWindow *foldersList = new foldersListWindow( this );
    foldersList->run_form();
    foldersList->show();
}

void folderIndex::on_indexFtpButton_clicked()
{
    inputFolderDialog inputFolder( this );
    inputFolder.run_form();
}

QString folderIndex::getLastSourceFolder() const
{
    QString retStr;

    QSettings settings("Folder index", "Settings");
    settings.beginGroup("Folders");
        retStr = settings.value("last_source_folder", "" ).toString();
    settings.endGroup();

    return retStr;
}

void folderIndex::on_indexFolderButton_clicked()
{
    QString dirPath = QFileDialog::getExistingDirectory ( this,
                                                          "Choose source folder",
                                                          getLastSourceFolder() );
    if ( !dirPath.isEmpty() )
    {
        setLastSourceFolder( dirPath );

        MainWindow * listWind = new MainWindow();
        listWind->show();
        listWind->run_folder( dirPath );
    }
}

void folderIndex::setLastSourceFolder( const QString &path )
{
    QSettings settings("Folder index", "Settings");
    settings.beginGroup("Folders");
    settings.setValue("last_source_folder", path );
    settings.endGroup();
}

void folderIndex::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    if (reason == QSystemTrayIcon::DoubleClick )
        this->showNormal();
}

// ---------------------------------- custom events -------------------------------------
void folderIndex::closeEvent(QCloseEvent *event)
{
    if (trayIcon->isVisible())
    {
        hide();
        event->ignore();
        if ( !notifyShown )
        {
            trayIcon->showMessage( "FolderIndex v.1.0",
                                   "The program will keep running in the "
                                   "system tray. To terminate the program, "
                                   "choose Exit.", QSystemTrayIcon::Information,
                                   2000);
            notifyShown = true;
        }
    }
}

void folderIndex::on_actionOpen_triggered()
{
    QSettings settings("Folder index", "Settings");
    settings.beginGroup("Folders");
    QString last_read_folder = settings.value("last_read_folder", "" ).toString();
    settings.endGroup();

    QString fileName = QFileDialog::getOpenFileName(this, tr("Open file"),
                                                    last_read_folder,
                                                    tr("Listing files (*.findex)"));

    if ( fileName.isEmpty() )
        return;

    if ( !fileName.isEmpty() && fileName.right(6) == "findex" )
    {
        QSettings settings("Folder index", "Settings");
        settings.beginGroup("Folders");
        settings.setValue("last_read_folder", fileName.left(fileName.lastIndexOf('/') ) );
        settings.endGroup();

        MainWindow * listWind = new MainWindow();
        listWind->show();
        listWind->open_form( fileName );
    }
    else if ( fileName.right(6) != "findex" )
        QMessageBox::critical( this, "FolderIndex", "Chosen file has insupported format" );
}

void folderIndex::on_actionSettings_triggered()
{
    myViewsForm->run_form();
}
